<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\user;


use app\model\tool\UserToolArticleModel;
use app\model\user\UserInviteRelationModel;
use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\consume\ConsumePointLogModel;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\novel\NovelSpreadModel;
use plugin\admin\app\model\reward\RewardPlanModel;
use plugin\admin\app\model\tool\UserToolVideoModel;
use plugin\admin\app\model\user\UserIncomeFlowModel;
use plugin\admin\app\model\user\UserModel;
use plugin\admin\app\model\user\UserPointFlowModel;
use plugin\admin\app\model\user\UserRechargeOrderModel;
use plugin\admin\app\model\vip\ReceiveVipByMobileModel;
use plugin\admin\app\model\withdraw\UserWithdrawModel;
use support\Db;
use support\Request;
use support\Response;

class PointLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new UserPointFlowModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }

        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        $tables = [];
        foreach ($items as $val){
            $tables[$val['source_table']][] = $val['sourceid'];
        }

        $orderMap = [];
        $consumeMap = [];
        $novelMap = [];
        $tableDatas = [
            UserRechargeOrderModel::getInstance()->getTable(),
            ConsumePointLogModel::getInstance()->getTable(),
        ];
        foreach($tableDatas as $table){
            if(!isset($tables[$table])){
               continue;
            }
            $ids = $tables[$table];
            if($table==UserRechargeOrderModel::getInstance()->getTable()){
                $orderData = UserRechargeOrderModel::getInstance()->getWhere([['id','in',$ids]])->get();
                $orderMap = array_column($orderData->toArray(),null,'id');
            }elseif($table==ConsumePointLogModel::getInstance()->getTable()){
                $consumeData = ConsumePointLogModel::getInstance()->getWhere([['id','in',$ids]])->get();
                $consumeMap = array_column($consumeData->toArray(),null,'id');
                $novelMap = $this->getListMapById(NovelSpreadModel::getInstance(),$consumeData->toArray(),'novel_sn','novel_sn');
            }
        }

        foreach($items as &$val){
          if($val['source_table']==UserRechargeOrderModel::getInstance()->getTable()){
              if(isset($orderMap[$val['sourceid']]) && $orderMap[$val['sourceid']]['product_type']==1){
                  $val['name'] = '开通会员： 订单号:'.$orderMap[$val['sourceid']]['order_no'];
              }elseif(isset($orderMap[$val['sourceid']]) && $orderMap[$val['sourceid']]['product_type']==2){
                  if($val['way']==1){
                      $val['name'] = '购买点数： 订单号:'.$orderMap[$val['sourceid']]['order_no'];
                  }else{
                      $val['name'] = '退款-购买点数返还： 订单号:'.$orderMap[$val['sourceid']]['order_no'];
                  }

              }
          }elseif($val['source_table']==ReceiveVipByMobileModel::getInstance()->getTable()){
              $val['name'] = '体验一天会员： 赠送点数';
          }elseif($val['source_table']==UserModel::getInstance()->getTable()){
              $val['name'] = '注册新用户： 赠送点数';
          }elseif($val['source_table']==ConsumePointLogModel::getInstance()->getTable()){
              if(isset($consumeMap[$val['sourceid']]) && $consumeMap[$val['sourceid']]['plan_type']==1){
                  if(isset($novelMap[$consumeMap[$val['sourceid']]['novel_sn']])){
                      $val['name'] = '消费小说： 小说编号 '.($novelMap[$consumeMap[$val['sourceid']]['novel_sn']]['title']??'');
                  }else{
                      $val['name'] = '消费小说： 小说编号 '.($consumeMap[$val['sourceid']]['novel_sn']??'');
                  }
              }elseif(isset($consumeMap[$val['sourceid']]) && $consumeMap[$val['sourceid']]['plan_type']==2){
                  $val['name'] = '消费漫画推文：ID '.$consumeMap[$val['sourceid']]['articleid'];
              }elseif(isset($consumeMap[$val['sourceid']]) && $consumeMap[$val['sourceid']]['plan_type']==3){
                  $val['name'] = '消费爆款二创：ID '.$consumeMap[$val['sourceid']]['videoid'];
              }elseif(isset($consumeMap[$val['sourceid']]) && $consumeMap[$val['sourceid']]['plan_type']==4){
                  if($consumeMap[$val['sourceid']]['source_table']==UserToolVideoModel::getInstance()->getTable()){
                      $val['name'] = '消费视频去水印、提取文字工具：ID '.$consumeMap[$val['sourceid']]['sourceid'];
                  }elseif($consumeMap[$val['sourceid']]['source_table']==UserToolArticleModel::getInstance()->getTable()){
                      $val['name'] = '消费文章改编、文章分段工具：ID '.$consumeMap[$val['sourceid']]['sourceid'];
                  }else{
                      $val['name'] = '消费工具：'.$consumeMap[$val['sourceid']]['source_table'];
                  }
              }elseif(isset($consumeMap[$val['sourceid']]) && $consumeMap[$val['sourceid']]['plan_type']==5){
                  if(isset($novelMap[$consumeMap[$val['sourceid']]['novel_sn']])){
                      $val['name'] = '消费海外推文 · 一键推文：ID '.($consumeMap[$val['sourceid']]['overseaid']??'');
                  }elseif(empty($consumeMap[$val['sourceid']]['novel_sn'])){
                      $val['name'] = '消费海外推文 · 工具：ID '.($consumeMap[$val['sourceid']]['overseaid']??'');
                  }else{
                      $val['name'] = '消费海外推文：：ID '.($consumeMap[$val['sourceid']]['overseaid']??'');
                  }
              }
              $val['name'] =  ($val['name'].( $val['is_refund']==1?' · 失败返还':''));
          }

          $val['name'] = $val['name']??'';
        }
        return parent::formatNormal($items, $total);
    }

    public function insert(Request $request): Response
    {
        return parent::insert($request);
    }


    public function update(Request $request): Response
    {
        return parent::update($request);
    }

    public function delete(Request $request): Response
    {
        return parent::isDelete($request);
    }
}